home *** CD-ROM | disk | FTP | other *** search
- /**
- --
- -- App: Getting Started w/QD GX (WWDC)
- --
- --
- -- Version: 1.0 4/93: added all of the calls required to support the "Getting
- -- Started with QuickDraw™ GX" session at the WWDC '93
- --
- -- 8/93: updated file to work with the ß2 "GXified" interface files
- -- 3/94: dmh - general cleanup/debugging.
- --
- -- File: Getting Started GX - printing.c
- --
- --
- -- Comments: This code contains all of the functions required to print the QuickDraw GX shapes we created.
- -- We use the QuickDraw GX printing method which takes a picture and passes it to the QuickDraw GX
- -- printing system. The picture will always contains what is in the front window.
- --
- -- If QuickDraw GX printing systems has not been installed, the printing menu items in the File
- -- menu will be dis-abled.
- --
- --
- -- Components: Getting Started GX - main.c
- -- Getting Started GX - main.h
- -- Getting Started GX - shapes.c
- -- Getting Started GX - printing.c
- -- Getting Started GX - misc.c
- -- Getting Started QD GX.π.rsrc
- --
- -- The file titled: "Getting Started GX - main.c" contains the code required to
- -- intialize and tear down the QuickDraw GX world, and the event loop.
- --
- -- The file titled: "Getting Started GX - shapes.c" contains of the code used to
- -- create and manipulate the shapes draw into the window.
- --
- --
- -- QuickDraw GX
- -- Libraries
- -- Used: This application uses the following QuickDraw GX library code files:
- -- "color library.c", "font library.c", "graphics debug library.c",
- -- "layout library.c", "qd library.c", "shape library.c",
- -- "transferMode library.c", and "transform library.c".
- --
- --
- -- Notes: 1) Print this file in landscape for the best results
- -- 2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
- -- 3) This code was adapted from the "Banana Jr." QuickDraw GX sample.
- --
- --
- -- Author: Pete "Luke" Alexander
- -- Developer Technical Support
- -- AppleLink: DEVSUPPORT
- --
- --
- -- ©1992 - 1994 Apple Computer, Inc.
- -- All rights reserved.
- --
- **/
-
- #include "PrintingManager.h"
- #include "GXExceptions.h"
- #include "memory.h"
- #include "Getting Started GX - main.h"
-
-
- /*------ SetUpEditMenuRec ------------------------------------------------------------------------------------*/
- //
- // This routine sets up an gxEditMenuRecord which references our edit menu. This structure
- // is used by the GXJobDefaultFormatDialog and GXJobPrintDialog calls to allow cut, copy, & paste
- // operations from the dialogs.
- //
- void SetUpEditMenuRec(gxEditMenuRecord *edMenuRec)
- {
-
- edMenuRec->editMenuID = mEdit;
- edMenuRec->cutItem = iCut;
- edMenuRec->copyItem = iCopy;
- edMenuRec->pasteItem = iPaste;
- edMenuRec->clearItem = iClear;
- edMenuRec->undoItem = iUndo;
- }
-
-
- /*------ DoFormat ------------------------------------------------------------------------------------*/
- //
- // This routine performs GX's equivalent of the Page Setup (PrStlDialog) call of
- // the old printing architecture.
- //
- OSErr DoFormat(WindowPtr theWindow, gxDialogResult *result)
- {
- OSErr err;
- gxEditMenuRecord edMenuRec;
- gxJob documentJob;
-
- //
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the job
- // format dialog. Remember to check for errors.
- //
- if (theWindow)
- {
- documentJob = GetDocJob(theWindow);
- SetUpEditMenuRec(&edMenuRec);
- *result = GXJobDefaultFormatDialog(documentJob, &edMenuRec);
- }
-
- return GXGetJobError(documentJob);
- }
-
-
- /*------ DoPrinting ----------------------------------------------------------------------------------*/
- //
- // This routine performs QuickDraw GX's equivalent of the Print gxJob Dialog (PrJobDialog) call of
- // the old printing architecture, and then prints the document if the user wants to.
- //
- OSErr DoPrinting(WindowPtr theWindow)
- {
- OSErr err = noErr;
- gxDialogResult result;
- gxEditMenuRecord edMenuRec;
- gxJob docJob;
-
-
- //
- // If we have a non-nil WindowPtr, set up our edit menu record and handle the print
- // job dialog. Remember to check for errors. If no errors occur, and the user clicks
- // ok, print the window's document.
- //
- if (theWindow)
- {
- docJob = GetDocJob(theWindow);
-
- SetUpEditMenuRec(&edMenuRec);
- result = GXJobPrintDialog(docJob, &edMenuRec);
- err = GXGetJobError(docJob);
-
- if ((result == gxOKSelected) && !(err))
- err = DoPrintLoop(theWindow);
- }
-
- return err;
- }
-
-
-
- /*------ DoPrintLoop ----------------------------------------------------------------------------------*/
- //
- // This routine prints one copy of the window's document using whatever job and format
- // is currently attached to it. (If the window wwere just created and then the user
- // selected Print One Copy w/o first selecting Page Setup…, the system default job and
- // format are stored with this document.
- //
- OSErr DoPrintLoop(WindowPtr theWindow)
- {
- OSErr err = noErr;
- gxJob documentJob;
- Str255 windowTitle;
-
- //
- // If we have a non-nil WindowPtr, start the print job, print a page and then finish
- // the job. Since we have just a one page document, we don't bother counting pages.
- // Remember to check those errors!
- //
- if (theWindow)
- {
- documentJob = GetDocJob(theWindow);
- GetWTitle(theWindow, windowTitle);
-
- GXStartJob(documentJob, windowTitle, 1);
- err = GXGetJobError(documentJob);
-
- if (!err)
- {
- GXPrintPage(documentJob, 1,GXGetJobFormat(documentJob, 1), GetDocShape(theWindow));
- err = GXGetJobError(documentJob);
- }
-
- GXFinishJob(documentJob);
- if (!err) err = GXGetJobError(documentJob);
- }
- return err;
- }
-
-
-
- /*******************************************************************
- DoPrintOneCopy sets up our job collection items for printing
- one copy of a document, and then prints the document.
-
- ********************************************************************/
-
- OSErr DoPrintOneCopy(WindowPtr wind)
- {
- OSErr err;
- Collection jobCollection;
- gxCopiesInfo copiesInfo;
- gxFileDestinationInfo destInfo;
- gxPageRangeInfo pageRangeInfo;
- Ptr oldCopiesInfo = nil, oldPageRangeInfo = nil, oldDestInfo = nil;
- long oldCopiesSize, oldPageRangeInfoSize, oldDestInfoSize;
- TH_Doc doc = (TH_Doc) GetWRefCon(wind);
-
- /* Get the job collection and set it up to print one copy… */
-
- jobCollection = GXGetJobCollection(GetDocJob(wind));
-
-
- /* Set number of copies to 1. */
-
- copiesInfo.copies = 1;
- err = MyReplaceCollectionItem(&copiesInfo, sizeof(gxCopiesInfo),
- gxCopiesTag, gxPrintingTagID,
- jobCollection, &oldCopiesInfo, &oldCopiesSize);
- nrequire(err, ReplaceCopies_error);
-
-
- /* Set page range to "all". */
-
- pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
- pageRangeInfo.minFromPage = 1;
- pageRangeInfo.simpleRange.fromPage = 1; // This app only handles one page docs.
- pageRangeInfo.maxToPage = 1;
- pageRangeInfo.simpleRange.toPage = 1;
- pageRangeInfo.simpleRange.printAll = true;
- err = MyReplaceCollectionItem(&pageRangeInfo, sizeof(gxPageRangeInfo),
- gxPageRangeTag, gxPrintingTagID,
- jobCollection, &oldPageRangeInfo, &oldPageRangeInfoSize);
- nrequire(err, ReplacePageRange_error);
-
-
- /* Set destination to "printer". */
-
- destInfo.toFile = false;
- err = MyReplaceCollectionItem(&destInfo, sizeof(gxFileDestinationInfo),
- gxFileDestinationTag, gxPrintingTagID,
- jobCollection, &oldDestInfo, &oldDestInfoSize);
- nrequire(err, ReplaceDestination_error);
-
-
- /* Print one copy of our document. */
-
- err = DoPrintLoop(wind);
-
-
- /* Restore original number of copies, page range, and output
- destination in case anybody uses that info. */
-
- ReplaceCopies_error:
- MyReplaceCollectionItem(oldCopiesInfo, oldCopiesSize,
- gxCopiesTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
- ReplacePageRange_error:
- MyReplaceCollectionItem(oldPageRangeInfo, oldPageRangeInfoSize,
- gxPageRangeTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
- ReplaceDestination_error:
- MyReplaceCollectionItem(oldDestInfo, oldDestInfoSize,
- gxFileDestinationTag, gxPrintingTagID,
- jobCollection, nil, nil);
-
-
- /* Dispose of the pointers that MyReplaceCollectionItem created. */
-
- if (oldCopiesInfo)
- DisposePtr(oldCopiesInfo);
-
- if (oldPageRangeInfo)
- DisposePtr(oldPageRangeInfo);
-
- if (oldDestInfo)
- DisposePtr(oldDestInfo);
-
- return err;
- }
-
-
-
- /*******************************************************************
- MyReplaceCollectionItem is a generic routine that replaces a
- collection item with the passed data. If the oldData parameter
- is not nil, this routine creates a pointer and returns the data
- that's being replaced in it. (If the item doesn't already exist,
- then a copy of the newData is returned instead.)
-
- ********************************************************************/
-
- OSErr MyReplaceCollectionItem(void *newData, long collectSize,
- OSType collectType, long collectID,
- Collection whichCollection,
- Ptr *oldData, long *oldDataSize)
- {
- OSErr err = noErr;
- long index;
-
- /*
- If we're supposed to return the old data, get it.
- If there is no old data, return a copy of the new data.
- */
-
- if (oldData)
- {
- err = GetCollectionItemInfo(whichCollection,
- collectType,
- collectID,
- dontWantIndex,
- oldDataSize,
- dontWantAttributes);
-
- if (err)
- {
- *oldDataSize = collectSize;
- *oldData = NewPtrSys(*oldDataSize);
- if (!(err = MemError()))
- BlockMove(newData, *oldData, collectSize);
- }
- else
- {
- *oldData = NewPtrSys(*oldDataSize);
- if (!(err = MemError()))
- err = GetCollectionItem(whichCollection,
- collectType,
- collectID,
- dontWantSize,
- *oldData);
- }
- }
-
- nrequire(err, CouldNotSetOldData);
-
-
- // If we're adding a new collection item, do so. Otherwise, get the
- // existing item's index and replace the old collection item.
-
- err = AddCollectionItem(whichCollection,
- collectType,
- collectID,
- collectSize,
- newData);
-
- if (err == collectionItemLockedErr)
- {
- err = GetCollectionItemInfo(whichCollection,
- collectType,
- collectID,
- &index,
- dontWantSize,
- dontWantAttributes);
- if (!err)
- err = ReplaceIndexedCollectionItem(whichCollection,
- index,
- collectSize,
- newData);
- }
-
- CouldNotSetOldData:
- return err;
- }
-
-